<?php

/**
 * 后台举报控制器
 */
class PunishController extends AdminBase {

    /**
     * 举报列表
     * @return [type] [description]
     */
    public function actionIndex() {
        $objModel = new PunishModel('search');
        //重置其值
        foreach ($objModel->attributes as $key => $value) {
            $objModel->$key = null;
        }

        $type = isset($_GET['type']) ? $_GET['type'] : '';
        $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';

        if (isset($_GET['PunishModel']) || isset($_GET['type'])) {

            switch ($_GET['type']) {
                case 1:
                    $_GET['PunishModel']['uid'] = $_GET['keyword'];
                    break;
                case 2:
                    $_GET['PunishModel']['username'] = $_GET['keyword'];
                    break;
                case 3:
                    $_GET['PunishModel']['good_id'] = $_GET['keyword'];
                    break;
                case 4:
                    $_GET['PunishModel']['goods_name'] = $_GET['keyword'];
                    break;
                case 5:
                    $_GET['PunishModel']['admin_id'] = $_GET['keyword'];
                    break;
            }

            $objModel->attributes = $_GET['PunishModel'];
        }


        $objCriteria = $objModel->createSearchCriteria();


        //分页处理''''''''''''
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //分页数
        $limit = 20;
        //信息总数
        $count = $objModel->count($objCriteria);

        //分页
        $page = self::page($count, $limit, $pageId);
        //查询信息
        $objCriteria->limit = $page->listRows;
        $objCriteria->offset = $page->firstRow;
        $objCriteria->order = 't.status asc,t.id desc';

        //排序(以后估计要用到)
        $objSort = new CSort('PunishModel');
        $objSort->applyOrder($objCriteria);



        $objModels = $objModel->with(array('GoodsModel' => array('select' => 'detail_url,product_id')))->findAll($objCriteria);

        $this->assign('count', $count);
        $this->assign('type', $type);
        $this->assign('keyword', $keyword);
        $this->assign('Page', $page->show());
        $this->assign('sort', $objSort);
        $this->assign('model', $objModel);
        $this->assign('data', $objModels);
        $this->render('index');
    }

    /**
     * 改变状态
     * @return [type] [description]
     */
    public function actionEditStatus() {
        $id = Yii::app()->request->getParam('id');
        $status = Yii::app()->request->getParam('status');
        $reason = Yii::app()->request->getParam('reason');

        $objModel = PunishModel::model()->findByPk($id);
        if (!$objModel) {
            $this->error('找不到此记录');
        }

        if ($objModel->status != PunishModel::STATUS_NOT) {
            $this->error('已经处理了');
        }

        //处理
        $objModel->status = empty($status) ? PunishModel::STATUS_HAS : $status;
        $objModel->admin_id = Yii::app()->passport['admin_id'];
        $objModel->reason = $reason;
        $objModel->save(false, array('status', 'admin_id', 'reason'));

        //取消或者忽略
        if ($objModel->status == PunishModel::STATUS_CANCEL) {
            if ($objModel->uid) {
                //站内通知
                $extendParams = array(
                    'title' => '商品举报处理结果通知',
                    'content' => '您举报的商品(' . $objModel->goods_name . ')经管理员确认受理不成功，原因：' . $objModel->reason . '',
                );
                MessageModel::model()->systemNotice($objModel->uid, $extendParams);
            }
        }

        if ($objModel->status == PunishModel::STATUS_HAS) {
            //举报送积分
            if ($objModel->uid) {
                $jifen = 10;
                MembersModel::model()->plusScore($objModel->uid, $jifen);
                MembersActionModel::model()->addAction($objModel->uid, MembersActionModel::action_punish, '商品举报成功，送' . $jifen . '个积分。');
                //经验值
                MembersModel::model()->plusExp($objModel->uid, 2, '商品举报成功，经验值加2。');
                //站内通知
                $extendParams = array(
                    'title' => '商品举报处理结果通知',
                    'content' => '您举报的商品(' . $objModel->goods_name . ')经管理员审核，确认属实奖励' . $jifen . '个积分。',
                );
                MessageModel::model()->systemNotice($objModel->uid, $extendParams);
            }
        }

        $arrResult = array(
            'status' => true,
            'info' => '处理成功',
            'reason' => $objModel->reason,
        );
        echo json_encode($arrResult);
        //$this->success('处理成功');
    }

}
